﻿using HCApp.UI.Windows.Views;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Linq;
using System.Collections.Generic;
using Syncfusion.Windows.Tools.Controls;

namespace MainAppEntryPrismWpf.Views
{
    /// <summary>
    /// Interaction logic for GGManagePage
    /// </summary>
    public partial class GGManagePage : ContentPageBase
    {

        public DataClassesSCDBDataContext SCDBDataContent;

        private int MaxBH = -1;

        private DSZX_CPGGB SelectedYG = new DSZX_CPGGB();

        private List<DSZX_CPGGB> ygxxList;    //所有列表  同数据库中数据同

        public GGManagePage()
        {
            InitializeComponent();

            SCDBDataContent = new DataClassesSCDBDataContext();

            // datagrid.ItemsSource = (this.datagrid.DataContext as SCDB.ViewModels.UserInfoViewModel).Users;

            datagrid.ItemsSource = (from data in SCDBDataContent.DSZX_CPGGB
                                    where data.CJH== "单丝"
                                    select data).ToList();
        }

        private void CheckBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            var checkBox = sender as CheckBox;
            if (checkBox != null)
            {
                checkBox.IsChecked = !checkBox.IsChecked;
            }

        }


        private void ClearControls()
        {
            this.ggTB.Text = "";
            this.jgTB.Text = "";

            
        //    this.cjSelect.SelectedIndex = 0;


        }

        private void UpdateControls()
        {
            if (SelectedYG != null)
            {
                this.ggTB.Text = SelectedYG.CPGG.ToString();
                this.jgTB.Value = SelectedYG.CPGJ;              
           //     this.cjSelect.SelectedIndex = SelectedYG.CJH.ToString() == "单丝" ? 0 : 1;
            }

        }

        private int gzToIndex(string gzString)
        {
            int kindIndex = -1;
            switch (gzString)
            {
                case "工种1":
                    kindIndex = 0;
                    break;
                case "工种2":
                    kindIndex = 1;
                    break;
                case "工种3":
                    kindIndex = 2;
                    break;
                default:
                    break;
            }
            return kindIndex;

        }

        private int jsdjToIndex(int jsdj)

        {
            return jsdj - 1;

        }


        private int GexMaxBH()
        {
            var MaxBHBody = SCDBDataContent.DSZX_YGXXB.OrderByDescending(x => x.YGBH).FirstOrDefault();
            if (MaxBHBody == null)
            {
                MaxBH = 2001;
                return -1;

            }
            else
            {
                MaxBH = (int)MaxBHBody.YGBH;
                return 1;
            }

        }
        private void inquiry_Click(object sender, RoutedEventArgs e)
        {

            ygxxList = SCDBDataContent.DSZX_CPGGB.Where(i => i.CPGG.Contains(this.ggTB.Text.ToString())&& i.CJH=="单丝").ToList();
            datagrid.ItemsSource = ygxxList;

        }

        private void add_Click(object sender, RoutedEventArgs e)
        {
           // var fdfCJH = (this.cjSelect.SelectedItem as ComboBoxItem).Content.ToString();
            GexMaxBH();

            var currentYG = new DSZX_CPGGB()
            {
                
                CPGG = this.ggTB.Text,
                CPGJ = this.jgTB.Value,
                // CJH = (this.cjSelect.SelectedItem as ComboBoxItem).Content.ToString(),
                 CJH = "单丝", 
            };

            SCDBDataContent.DSZX_CPGGB.InsertOnSubmit(currentYG);
            SCDBDataContent.SubmitChanges();

            ygxxList = (from data
                       in SCDBDataContent.DSZX_CPGGB
                        where data.CJH == "单丝"
                        select data).ToList();
            datagrid.ItemsSource = ygxxList;
        }

        private void modify_Click(object sender, RoutedEventArgs e)
        {
            if (SelectedYG != null)
            {
                var toModifyObject = SCDBDataContent.DSZX_CPGGB.Where(a => a.ID == SelectedYG.ID).FirstOrDefault();

                if (toModifyObject != null)
                {
                    
                    toModifyObject.CPGG = this.ggTB.Text.ToString();
                    toModifyObject.CPGJ = this.jgTB.Value;
                    toModifyObject.CJH = "单丝";
                   

                    SCDBDataContent.SubmitChanges();

                    ygxxList = (from data
                              in SCDBDataContent.DSZX_CPGGB
                                where data.CJH == "单丝"
                                select data).ToList();
                    datagrid.ItemsSource = ygxxList;

                }
                else
                {
                    MessageBox.Show("没有找到！！！");
                }
            }





        }

        private void delete_Click(object sender, RoutedEventArgs e)
        {
            if (SelectedYG != null)
            {
                var toModifyObject = SCDBDataContent.DSZX_CPGGB.Where(a => a.ID == SelectedYG.ID).FirstOrDefault();

                if (toModifyObject != null)
                {
                    SCDBDataContent.DSZX_CPGGB.DeleteOnSubmit(toModifyObject);

                    SCDBDataContent.SubmitChanges();

                    ygxxList = (from data
                              in SCDBDataContent.DSZX_CPGGB
                                where data.CJH == "单丝"
                                select data).ToList();
                    datagrid.ItemsSource = ygxxList;
                }
                else
                {
                    MessageBox.Show("没有找到！！！");
                }
            }

        }

        private void datagrid_SelectionChanged(object sender, Syncfusion.UI.Xaml.Grid.GridSelectionChangedEventArgs e)
        {

            SelectedYG = this.datagrid.SelectedItem as DSZX_CPGGB;

            if (SelectedYG != null)
            {
                UpdateControls();
            }
            else
            {
                ClearControls();
            }
            //更新控件信息，便于修改用户信息

        }

        private void cjSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
